﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebReport.Reports
{
    public partial class RequestDetail : System.Web.UI.Page
    {

        protected string Url
        {
            get
            {
                string url = Request.QueryString["url"];
                if (string.IsNullOrEmpty(url))
                    return "";
                else
                    return url;
            }
        }
        protected DateTime? BeginTime
        {
            get
            {
                string time = Request.QueryString["btime"];
                if (string.IsNullOrEmpty(time))
                    return null;
                else
                    return Convert.ToDateTime(time);
            }
        }
        protected DateTime? EndTime
        {
            get
            {
                string time = Request.QueryString["etime"];
                if (string.IsNullOrEmpty(time))
                    return null;
                else
                    return Convert.ToDateTime(time);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (BeginTime == null || EndTime == null)
            {
                lblMessage.Text = "Please select item from WebPerformance page.";
            }
            else
                BindData();
        }
        protected void BindData()
        {
            using (DefaultDBEntity db = new DefaultDBEntity())
            {

                var query = from p in db.Request
                            where p.RequestTime >= BeginTime && p.RequestTime <= EndTime
                            select new 
                            { 
                                p.URL,
                                p.RequestTime,
                                p.GatewayDeterminationTime,
                                p.TCPConnectTime,
                                p.ClientTime,
                                p.NetworkTime,
                                p.ServerTime,
                                p.ProcessId
                            };
                gvRequest.DataSource = query.ToList();
                gvRequest.DataBind();
            }
            if (gvRequest.Rows.Count <= 0)
            {
                lblMessage.Text = "NO Data...";
            }
        }

        protected void gvRequest_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string url = e.Row.Cells[0].Text;
                int length = url.Length > 15 ? 15 : url.Length;
                e.Row.Cells[0].Text = url.Substring(0, length)+"...";
                e.Row.Cells[0].ToolTip = url;
                //var item = e.Row.DataItem as RequestView;
                //if (item != null)
                //{
                //    e.Row.Cells[0].ToolTip = item.URL;
                //    int length = item.URL.Length > 6 ? 6 : item.URL.Length;
                //    e.Row.Cells[0].Text = item.URL.Substring(0, length) + "...";
                //    HyperLink btnDetail = e.Row.Cells[6].FindControl("btnDetail") as HyperLink;
                //    btnDetail.NavigateUrl = string.Format("RequestDetail.aspx?btime={0}&etime={1}&keepThis=true&TB_iframe=true&height=500&width=1000", item.BeginTime, item.EndTime);
                //}
            }
        }
    }
   
}